package clt.com.service.common.impl;

import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;

import clt.com.dao.impl.BaseDaoImpl;
import clt.com.service.common.ICacheService;
import clt.com.util.DateUtil;
import clt.com.util.DateUtil.DATE_PATTERN;
import clt.com.util.StringUtil;

import java.util.Date;

/**
 * 基础服务
 */
public class BaseServiceImpl {
	private static final Logger LOGGER = Logger.getLogger ( DateUtil.class.getName () );
	private BaseDaoImpl baseDao;// 用户中心操作的dao
	private ICacheService cacheService;


	public BaseDaoImpl getBaseDao(Map<String, String> map) {
		getBaseDao(map.get("dataSource"));
		return baseDao;
	}
	
	public BaseDaoImpl getBaseDao(String dataSource) {
		return baseDao;
	}
	
	public BaseDaoImpl getBaseDao() {
		
		return baseDao;
	}

	public void setBaseDao(BaseDaoImpl baseDao) {
		this.baseDao = baseDao;
	}

	public ICacheService getCacheService() {
		return cacheService;
	}

	public void setCacheService(ICacheService cacheService) {
		this.cacheService = cacheService;
	}


	/**
	 * 主键生成策略
	 * 
	 * @param tableName
	 *            需要获取主键的表名
	 */
	public String getSequence(String tableName) throws Exception {
		if (StringUtil.isEmpty(tableName)) {
			throw new Exception("获取主键时前缀不能为空!建议传入表名");
		}
		String id = null;
		try {
			String bgnStr  = "开始获取主键 ";
			LOGGER.info(bgnStr);
			id = DateUtil.date2String(new Date(),
					DATE_PATTERN.YYYYMMDDHHMMSSSSS)
					+ ""
					+(int)(Math.random()*90000+10000);
			String strId = "获取主键成功"+ "id=" + id;
			LOGGER.info(strId);
		} catch (Exception e) {
			LOGGER.error(e);
			id = UUID.randomUUID().toString();
			String errStr = "NOT ERROR! 主键获取成功:"+ ",id=" + id;
			LOGGER.error(errStr);
		}
		return id;
	}



}